package com.martian.common.util;

import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author martian
 * @version V1.0.0
 * @Description
 * @date 2023-03-24 14:37
 */

public class RedisUtil {

    public static boolean exists(StringRedisTemplate redisTemplate, String key) {
        return redisTemplate.hasKey(key);
    }

    public static<T> void set (StringRedisTemplate redisTemplate, String key, T  value, int timeout, TimeUnit range) {
        redisTemplate.opsForValue().set(key, JsonUtil.toJson(value),timeout, range);
    }

    public static<T> void setZSets(StringRedisTemplate redisTemplate, String key, String value){
        redisTemplate.opsForZSet().add(key,value,System.currentTimeMillis());
    }

    public static<T> T get (StringRedisTemplate redisTemplate, String key, Class<T> clazz) {
        String value = redisTemplate.opsForValue().get(key);
        if (null != value) {
            return JsonUtil.toObject(value, clazz);
        }
        return null;
    }
    public static<T> T getZSet(StringRedisTemplate redisTemplate,String key, Class<T> clazz){
        T value = (T) redisTemplate.opsForZSet().range(key,0,-1);
        return value;
    }

    public static void del(StringRedisTemplate redisTemplate, String key) {
        redisTemplate.delete(key);
    }
}
